package com.arch.dev.client.view;

import java.util.Date;
import java.util.Set;

import com.arch.dev.client.BudgetTable;
import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.security.AuthService;
import com.arch.dev.client.security.AuthServiceAsync;
import com.arch.dev.client.view.ui.BudgetImageBundle;
import com.arch.dev.client.view.ui.ImageButton;
import com.arch.dev.client.vo.BudgetVO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.MultiSelectionModel;

public class AddBudgetView extends Composite {
	
	private final AuthServiceAsync authService = GWT
			.create(AuthService.class);

	@UiField
	public BudgetTable budgetTable;

	@UiField
	public FormView formView;

	private static AddBudgetViewUiBinder uiBinder = GWT
			.create(AddBudgetViewUiBinder.class);

	interface AddBudgetViewUiBinder extends UiBinder<Widget, AddBudgetView> {
	}	

	public AddBudgetView() {

		initWidget(uiBinder.createAndBindUi(this));

		formView.saveButtonAddClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				BudgetController.getInstance().persistValues(
						AddBudgetView.this, formView.populateVo());

			}
		});
		
		formView.getCancelButton().setVisible(false);
		BudgetImageBundle bundle = (BudgetImageBundle) GWT

				.create(BudgetImageBundle.class);

		ImageButton deleteImageButton  = new ImageButton(null, bundle.getDeleteIcon());
		

		deleteImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				MultiSelectionModel<BudgetVO> multiSelectionModel = budgetTable
						.getMultiSelectionModel();
				Set<BudgetVO> budgetVOs = multiSelectionModel.getSelectedSet();
				multiSelectionModel.clear();

				BudgetController.getInstance().deleteValues(AddBudgetView.this,
						budgetVOs);

			}
		});
		
		deleteImageButton.setTitle("Delete Records");
		
		budgetTable.addToolBarWidget(deleteImageButton);
		
		ImageButton uploadImageButton  = new ImageButton(null, bundle.getUploadIcon());
		
		final UpLoadExcelDialogView upLoadExcelDialogView = new UpLoadExcelDialogView(this);
		
		uploadImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				upLoadExcelDialogView.show();

			}
		});
		
		uploadImageButton.setTitle("Upload Excel File");
		
		budgetTable.addToolBarWidget(uploadImageButton);
		
	final DateSearchView dateSearchView = new DateSearchView(DateSearchView.Types.DATE, this);
		
		dateSearchView.addSearchButtonHandler(new ClickHandler() {
			
			
			public void onClick(ClickEvent event) {
				BudgetController.getInstance().getDataForDate(new Date(dateSearchView.getDateBox().getValue()), AddBudgetView.this);
				
			}
		});
		
	
		
		budgetTable.addToolBarWidget(dateSearchView);
		


	}
	
	public void populateCatagoryListBox() {

		formView.populateCatagoryListBox();

	}

}
